<?php
/**

Plugin Name: PRose Flickr Gallery
Plugin URI: 
Description: Used with the Photo Concept Theme to add an interface to Flickr.
Version: 1.0.0
Author: Phil
Author URI: www.prose21.com
License: GPLv2 or later
*/

/*
* 1. 
* 2.  
*/

require_once (dirname(__FILE__).'/prose_flickr_calls.php');

// Functions to add the Settings Page to WordPress
function prose_flickr_pluggin_menu(){
	add_options_page( 'PRose Flickr Pluggin Options', 
			  'PRose Flickr', 
			  'manage_options', 
			  'prose_flickr_id',
			  'prose_flickr_pluggin_options');
}
add_action('admin_menu', 'prose_flickr_pluggin_menu');

function prose_flickr_pluggin_options(){
	$prose_flickr_api = "";
	$prose_flickr_secret = "";
	
	if(!current_user_can( 'manage_options' )){
		wp_die( __( 'Bummer, you do not have sufficient permissions to access this page.' ) );
	}
	if(!empty($_POST)){
		$prose_flickr_api = sanitize_text_field($_POST['flickr_api']);
		$prose_flickr_secret = sanitize_text_field($_POST['flickr_secret']);
		
		update_option("prose_flickr_api", $prose_flickr_api);
		update_option("prose_flickr_secret", $prose_flickr_secret);
	}
	else{	
		$prose_flickr_api = get_option("prose_flickr_api","");
		$prose_flickr_secret = get_option("prose_flickr_secret","");
	}
	
	include (dirname(__FILE__).'/prose_flickr_menu.php');
}

// Adding in the custom post type
function prose_codex_flickr_init(){
	$args = array(
		'public'	=> true,
		'label'		=> 'Flickr Posts',
		'menu_position'	=> '23',
		'has_archive'	=> true,
		'rewrite'	=> array(
					'slug'		=> 'Flickr_Post',
					'with_front'	=> false
				   ),
		'supports'	=> array(
					'title',
					'editor',
					'thumbnail'
				   )
	);
	register_post_type('prose_flickr_posts', $args);
}
add_action('init', 'prose_codex_flickr_init');

// Function to manage the metabox
// Function creating the metabox for Custom Post type Flickr Set
function prose_flickr_register_metabox(){
	add_meta_box('prose_flickr_list', 'Flickr Set', 'prose_create_flickr_metabox', 'prose_flickr_posts', 'side', 'default');
}
add_action('add_meta_boxes', 'prose_flickr_register_metabox');
	
// Function creating the data collection method for the metabox
function prose_create_flickr_metabox($post){
	$prose_flickr_api = get_option("prose_flickr_api","");
	$prose_flickr_secret = get_option("prose_flickr_secret","");

	$prose_flickr_set = get_post_meta($post->ID, '_prose_flickr_photoset' , true);
	$prose_flickr_username = get_post_meta($post->ID, '_prose_flickr_username' , true);
	
	if($prose_flickr_set == false){
		$prose_flickr_set = "";
	}
	if($prose_flickr_username == false){
		$prose_flickr_username = "";
	}
	include (dirname(__FILE__).'/prose_flickr_metabox.php');
}

// Function saving keys to the database
function prose_save_flickr_photoset($post_id, $post){
	if($post->post_type != 'prose_flickr_posts')
		return;

	if(isset($_POST['prose_flickr_photoset'])){
		update_post_meta($post_id, '_prose_flickr_photoset', $_POST['prose_flickr_photoset']);
	}
	
	if(isset($_POST['flickr_username'])){
		update_post_meta($post_id, '_prose_flickr_username', $_POST['flickr_username']);
	}
}
add_action('save_post', 'prose_save_flickr_photoset', 10, 2);

// Function injecting the flickr URL for photos
function prose_display_flickr_photoset($content){
	// Verify your not on the dashboard
	if(is_admin())
		return $content;
	
	// 
	$photoset_id = get_post_meta(get_the_ID(), '_prose_flickr_photoset', true);
	$prose_flickr_api = get_option("prose_flickr_api","");
	
	
	if(!empty($photoset_id) && !empty($prose_flickr_api)){
		$prose_flickr_set_information = prose_flickr_get_photoset_info($prose_flickr_api, $photoset_id);
		$prose_flickr_set_photos = prose_flickr_get_photos($prose_flickr_api, $photoset_id);
		$prose_flickr_photo_urls = prose_flickr_generate_photo_url($prose_flickr_set_photos);
		
		$content .= '<br/> <img src="http://' . $prose_flickr_photo_urls[0] . '" alt="' . $prose_flickr_set_information['title'] .'" /> <br/>';

//	The FancyBox (http://fancyapps.com/fancybox/) method 2 way of including the gallery from a single image preveiw...
//	<a class="open_fancybox" href="http://fancyapps.com/fancybox/demo/1_b.jpg"><img src="http://fancyapps.com/fancybox/demo/1_s.jpg" alt=""/></a>
//	$content .= '<br/> <a class="open_fancybox" href="http://' . $prose_flickr_photo_urls[0] . '"><img src="http://' . $prose_flickr_photo_urls . '" alt="" /></a> <br/>';

	}
	return $content;
}
add_filter('the_content', 'prose_display_flickr_photoset');

// function prose_flickr_jquery_method() {
//    wp_deregister_script( 'jquery' );
//    wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
//    wp_enqueue_script( 'jquery' );
// }    
 
// add_action('wp_enqueue_scripts', 'my_scripts_method');
?>

?>